import os
import sys
import string
sys.path.append('%s/library' % (Dir('#').abspath))
import configuration.pmalloc

Import('mainEnv')  # read only -- clone if need to modify

buildEnv = configuration.pmalloc.Environment(mainEnv, mainEnv['BUILD_CONFIG_NAME'])

OPTIMIZE_FLAG = '-fno-rtti -fno-exceptions -finline-functions'
WARNINGS = ' -w'
#'-Wall -Wpointer-arith  -Wconversion -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Woverloaded-virtual '
DISABLE_WARNINGS = []


if buildEnv['BUILD_DEBUG'] == True:
	CCFLAGS= ' -g -D_M_BUILD_DEBUG -D_MNEMOSYNE_BUILD_DEBUG '
else:
	CCFLAGS= ' -g -DNDEBUG -O2 ' + OPTIMIZE_FLAG

if buildEnv['BUILD_STATS'] == True:
	buildEnv.Append(CCFLAGS = ' -D_M_STATS_BUILD')

CCFLAGS = CCFLAGS + WARNINGS + string.join(DISABLE_WARNINGS, ' ') + ' -fgnu-tm -fpic -fno-omit-frame-pointer'
CCFLAGS = CCFLAGS + ' -m64 '

buildEnv.Append(CCFLAGS = CCFLAGS)
buildEnv.Append(CXXFLAGS = CCFLAGS)
buildEnv.Append(CPPPATH = ['#library/atomic_ops'])
buildEnv.Append(CPPPATH = ['#library/mtm/include'])
buildEnv.Append(CPPPATH = ['#library/mtm/include/mode'])
buildEnv.Append(CPPPATH = ['#library/mtm/include/mode/common'])
buildEnv.Append(CPPPATH = ['#library/mtm/include/mode/pwb-common'])
buildEnv.Append(CPPPATH = ['#library/mtm/include/mode/pwbetl'])
buildEnv.Append(CPPPATH = ['#library/mtm/include/sysdeps/linux'])
buildEnv.Append(CPPPATH = ['#library/mtm/include/sysdeps/x86'])
buildEnv.Append(CPPPATH = ['#library/mcore/include'])
buildEnv.Append(CPPPATH = ['#library/mcore/include/hal'])
buildEnv.Append(CPPPATH = ['#library/mcore/include/log'])
buildEnv.Append(CPPPATH = ['#library/pmalloc/include'])
buildEnv.Append(LINKFLAGS = ' -T '+ buildEnv['MY_LINKER_DIR'] + '/linker_script_persistent_segment_m64')

if mainEnv['ENABLE_FTRACE'] == True:
        buildEnv.Append(CCFLAGS = '-D_ENABLE_FTRACE')

C_SRC = Split("""
              src/pdlmalloc.c 
              src/vhalloc.c 
              src/vistaheap.c
              """)

CXX_SRC = Split("""
                src/arch-specific.cpp
                src/hoardheap.cpp
                src/persistentheap.cpp
                src/processheap.cpp
                src/superblock.cpp
                src/threadheap.cpp
                src/wrapper.cpp
                """)

#Not sure what to do with these files:
DEPRECATED_CXX_SRC = Split("""
                src/mallocwrap.cpp
                """)


SRC = C_SRC + CXX_SRC


if buildEnv['BUILD_LINKAGE'] == 'dynamic':
	pmallocLibrary = buildEnv.SharedLibrary('pmalloc', SRC)
else:
	pmallocLibrary = buildEnv.StaticLibrary('pmalloc', SRC)

Return('pmallocLibrary')
